package com.draven.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.common.BusinessException;
import com.draven.entity.AccountPassword;
import com.draven.mapper.AccountPasswordMapper;
import com.draven.service.AccountPasswordService;
import com.draven.util.PasswordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账号密码服务实现类
 */
@Service
public class AccountPasswordServiceImpl extends ServiceImpl<AccountPasswordMapper, AccountPassword> implements AccountPasswordService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(Long userId, String title, String username, String password, String url, String category, String remark) {
        AccountPassword accountPassword = new AccountPassword();
        accountPassword.setUserId(userId);
        accountPassword.setTitle(title);
        accountPassword.setUsername(username);
        accountPassword.setPassword(password); // 前端已加密
        accountPassword.setUrl(url);
        accountPassword.setCategory(category);
        accountPassword.setRemark(remark);

        save(accountPassword);
        return accountPassword.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Long id, Long userId, String title, String username, String password, String url, String category, String remark) {
        // 查询账号密码
        AccountPassword accountPassword = getDetail(id, userId);

        // 更新信息
        accountPassword.setTitle(title);
        accountPassword.setUsername(username);
        // 如果密码不为空，则更新密码
        if (StringUtils.isNotBlank(password)) {
            accountPassword.setPassword(password); // 前端已加密
        }
        accountPassword.setUrl(url);
        accountPassword.setCategory(category);
        accountPassword.setRemark(remark);

        updateById(accountPassword);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id, Long userId) {
        // 查询账号密码
        AccountPassword accountPassword = getDetail(id, userId);

        // 删除账号密码
        removeById(id);
    }

    @Override
    public AccountPassword getDetail(Long id, Long userId) {
        // 查询账号密码
        AccountPassword accountPassword = getById(id);
        if (accountPassword == null) {
            throw new BusinessException(400, "账号密码不存在");
        }

        // 校验所有权
        if (!accountPassword.getUserId().equals(userId)) {
            throw new BusinessException(403, "无权限操作");
        }

        return accountPassword;
    }

    @Override
    public Page<AccountPassword> page(Long userId, String title, String username, String category, Integer pageNum, Integer pageSize) {
        // 构建查询条件
        LambdaQueryWrapper<AccountPassword> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccountPassword::getUserId, userId);
        wrapper.like(StringUtils.isNotBlank(title), AccountPassword::getTitle, title);
        wrapper.like(StringUtils.isNotBlank(username), AccountPassword::getUsername, username);
        wrapper.eq(StringUtils.isNotBlank(category), AccountPassword::getCategory, category);
        wrapper.orderByDesc(AccountPassword::getCreateTime);

        // 分页查询
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public String generatePassword(Integer length) {
        return PasswordUtil.generateRandomPassword(length);
    }
    
    @Override
    public int countByUserId(Long userId) {
        LambdaQueryWrapper<AccountPassword> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccountPassword::getUserId, userId);
        return (int) count(wrapper);
    }
} 